<template>
	<view>
		<!-- 没有地址时显示按钮  有地址显示详细的地址 -->
		<view class="address-show-box" v-if="JSON.stringify(address)  == '{}'">
			<button type="primary" @click="getAddress">请选择收获地址</button>
		</view>
		<view class="address-info-box" v-else>
			<view class="row1">
				<view class="row1-left">
					<view class="username">收货人：<text>{{address.userName}}</text></view>
				</view>
				<view class="row1-right">
					<view class="phone">电话：<text>{{address.telNumber}}</text></view>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
			</view>
			<view class="row2">
				<view class="row2-address">收货地址：<text>{{addressStr}}</text></view>
			</view>
		</view>
		<image src="/static/cart_border@2x.png" class="address-border-img"></image>
	</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex'
	export default {
		name:"my-address",
		data() {
			return {
				// address:{}
			};
		},
		created() {
			console.log(this.$store);
		},
		computed:{
			...mapState('user_m',['address']),
			addressStr(){
				// cityName: (...)
/* countyName: (...)
detailInfo: (...)
errMsg: (...)
nationalCode: (...)
postalCode: (...)
provinceName: (...) */
				return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
			}
		},
		methods:{
			...mapMutations('user_m',['updateAddress']),
			async getAddress(){
				const [err,succ] = await uni.chooseAddress().catch(err=>err);
				console.log(succ);
				// 地址信息
				// console.log(result);
				// let arr = [1,2,3];
				/* let aa = arr[0]  1
				let bb = arr[1]  2 
				let cc = arr[2]  3 */
				// let [aa,bb] = arr;
				// console.log(aa,bb);
				// 如果成功 this.address = succ
				if(err === null && succ.errMsg == 'chooseAddress:ok' ){
					// this.address = succ;
					this.updateAddress(succ);
				}else{
					return uni.$showMsg()
				}
				
			}
		}
	}
	/* 
	 点击请选择收获地址按钮  
	 需要发送请求  请求uni-app自带的测试收货地址 uni.chooseAddress
	 获取到了用户的收货地址
	 公共数据 vuex --user_m----state  address
	 data address初始值
	 
	 address {}
	address有收货地址的详细信息   显示地址box
		没有收货地址的详细信息    显示的是按钮
		
		addres{}   {addxxxx}
		‘{'aa':'aa',"bb":'fe'}’  '{}'
		JSON.stringfy (address)  == '{}'
		
	将地址存储vuex 并且实现持久化存储
		addres  user_m
	 */
</script>

<style lang="scss">
.address-show-box{
	height: 100px;
	display: flex;
	justify-content: center;
	align-items: center;
}
.address-info-box{
	.row1{
		height: 40px;
		display: flex;
		justify-content: space-between;
		.row1-right{
			display: flex;
			flex-wrap: nowrap;
		}
	}
	.row2{
		height: 40px;
	}
}
.address-border-img{
	height: 5px;
	width: 100%;
	display: block;
}
</style>